<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java线程创建与管理的底层实现机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        
        .code-block pre {
            margin: 0;
            padding: 60px 24px 24px;
            overflow-x: auto;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 3s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.3; }
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin: 30px 0;
        }
        
        .floating-icon {
            position: absolute;
            opacity: 0.1;
            animation: float 6s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-20px); }
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0.1em 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 relative overflow-hidden">
        <div class="floating-icon top-10 left-10 text-6xl">
            <i class="fas fa-code"></i>
        </div>
        <div class="floating-icon bottom-10 right-10 text-6xl" style="animation-delay: 3s;">
            <i class="fas fa-cogs"></i>
        </div>
        <div class="container mx-auto px-6 relative z-10">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 text-center">
                Java线程创建与管理的底层实现机制
            </h1>
            <p class="text-xl md:text-2xl text-center max-w-4xl mx-auto opacity-90">
                深入探索Java并发编程的核心，理解线程生命周期与管理的奥秘
            </p>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Java中，线程的创建与管理是并发编程的核心内容，理解其背后的实现机制有助于更高效地进行多线程开发。Java通过<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Thread</code>类和<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Executor</code>框架提供了线程的创建与管理功能。
            </p>
        </div>

        <!-- Thread Lifecycle Visualization -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">线程生命周期可视化</h2>
            <div class="mermaid">
                graph LR
                    A[NEW<br/>新建] -->|start()| B[RUNNABLE<br/>可运行]
                    B -->|获得锁失败| C[BLOCKED<br/>阻塞]
                    C -->|获得锁| B
                    B -->|wait()| D[WAITING<br/>等待]
                    D -->|notify()| B
                    B -->|sleep()| E[TIMED_WAITING<br/>限时等待]
                    E -->|时间到| B
                    B -->|run()结束| F[TERMINATED<br/>终止]
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style B fill:#9f9,stroke:#333,stroke-width:2px
                    style C fill:#ff9,stroke:#333,stroke-width:2px
                    style D fill:#99f,stroke:#333,stroke-width:2px
                    style E fill:#f99,stroke:#333,stroke-width:2px
                    style F fill:#999,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Section 1: Thread Creation -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">
                <i class="fas fa-plus-circle text-purple-600 mr-3"></i>1. 线程的创建与启动流程
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                线程的创建可以通过两种方式：继承<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Thread</code>类和实现<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Runnable</code>接口。下面我们详细分析这两种方式的实现。
            </p>

            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl p-6 mb-6">
                <h3 class="text-2xl font-semibold mb-4">
                    1.1 继承Thread类创建线程
                </h3>
                <p class="text-gray-700 mb-4">
                    继承<code class="bg-white px-2 py-1 rounded">Thread</code>类创建线程时，首先需要重写<code class="bg-white px-2 py-1 rounded">run()</code>方法，然后通过调用<code class="bg-white px-2 py-1 rounded">start()</code>方法来启动线程。
                </p>
                
                <div class="code-block mb-4">
                    <pre><code class="language-java">public class MyThread extends Thread {
    @Override
    public void run() {
        // 线程执行的代码
        System.out.println("Thread is running...");
    }

    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();  // 启动线程
    }
}</code></pre>
                </div>

                <div class="highlight-box">
                    <h4 class="text-xl font-semibold mb-2">
                        <i class="fas fa-lightbulb mr-2"></i>关键点：start()方法
                    </h4>
                    <p class="relative z-10">
                        <code class="bg-white bg-opacity-20 px-2 py-1 rounded">start()</code>方法会在调用时由JVM触发，调用后会将当前线程状态从<code class="bg-white bg-opacity-20 px-2 py-1 rounded">NEW</code>变为<code class="bg-white bg-opacity-20 px-2 py-1 rounded">RUNNABLE</code>，然后进入调度队列等待CPU资源。
                    </p>
                </div>
            </div>

            <div class="bg-gradient-to-r from-blue-50 to-green-50 rounded-xl p-6">
                <h3 class="text-2xl font-semibold mb-4">
                    1.2 实现Runnable接口创建线程
                </h3>
                <p class="text-gray-700 mb-4">
                    通过实现<code class="bg-white px-2 py-1 rounded">Runnable</code>接口并将其传递给<code class="bg-white px-2 py-1 rounded">Thread</code>对象，Java允许线程任务的复用。
                </p>
                
                <div class="code-block">
                    <pre><code class="language-java">public class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("Thread is running via Runnable...");
    }

    public static void main(String[] args) {
        MyRunnable task = new MyRunnable();
        Thread thread = new Thread(task);  // 创建Thread对象并传入Runnable实例
        thread.start();  // 启动线程
    }
}</code></pre>
                </div>
            </div>
        </div>

        <!-- Section 2: Thread States -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 section-title">
                <i class="fas fa-sync-alt text-purple-600 mr-3"></i>2. 线程的状态与生命周期管理
            </h2>
            
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 mb-8">
                <div class="bg-gradient-to-br from-pink-400 to-pink-600 text-white p-6 rounded-xl">
                    <i class="fas fa-baby text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">NEW</h4>
                    <p>线程刚被创建，尚未启动</p>
                </div>
                <div class="bg-gradient-to-br from-green-400 to-green-600 text-white p-6 rounded-xl">
                    <i class="fas fa-running text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">RUNNABLE</h4>
                    <p>线程已启动，等待操作系统调度执行</p>
                </div>
                <div class="bg-gradient-to-br from-yellow-400 to-yellow-600 text-white p-6 rounded-xl">
                    <i class="fas fa-lock text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">BLOCKED</h4>
                    <p>线程由于竞争锁而被阻塞</p>
                </div>
                <div class="bg-gradient-to-br from-blue-400 to-blue-600 text-white p-6 rounded-xl">
                    <i class="fas fa-clock text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">WAITING</h4>
                    <p>线程等待其他线程的通知</p>
                </div>
                <div class="bg-gradient-to-br from-purple-400 to-purple-600 text-white p-6 rounded-xl">
                    <i class="fas fa-hourglass-half text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">TIMED_WAITING</h4>
                    <p>线程在特定时间段内等待</p>
                </div>
                <div class="bg-gradient-to-br from-gray-400 to-gray-600 text-white p-6 rounde